//
//  WXPMineMiCreditoView.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/17.
//
//

#import "WXPMineMiCreditoView.h"
#import "WXPReportFailurePopupWindowView.h"
#import "WXPCreditReportDetailVC.h"

@interface WXPMineMiCreditoView ()

@property (nonatomic, strong) WXPReportFailurePopupWindowView * viewPopup;

@end

@implementation WXPMineMiCreditoView

#pragma mark - lazy
- (UIView *)viewBack
{
    if (_viewBack == nil) {
        _viewBack = [UIView  new];
        _viewBack.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, 0);
        _viewBack.backgroundColor = COLOR_WHITE;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
    }
    return  _viewBack;
}
- (UILabel *)labelTitle{
   if (_labelTitle == nil) {
       _labelTitle = [UILabel new];
       _labelTitle.textColor = COLOR_BLACK;
       _labelTitle.font = [UIFont boldSystemFontOfSize:16];
       [_labelTitle fitTitle:@"Mi crédito" variable:0];
   }
   return _labelTitle;
}
- (UIView *)lineTitle{
   if (_lineTitle == nil) {
       _lineTitle = [UIView new];
       _lineTitle.widthHeight = XY(36, 6);
       [GlobalMethod setRoundView:_lineTitle color:[UIColor clearColor] numRound:6/2 width:0];
       _lineTitle.backgroundColor = COLOR_PRIMARY;
   }
   return _lineTitle;
}
- (UIView *)lineView{
   if (_lineView == nil) {
       _lineView = [UIView new];
       _lineView.backgroundColor = COLOR_LINE;
   }
   return _lineView;
}
- (UIView *)viewCloudun
{
    if (_viewCloudun == nil) {
        _viewCloudun = [UIView  new];
        _viewCloudun.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
        [GlobalMethod setRoundView:_viewCloudun color:[UIColor clearColor] numRound:16 width:0];
        _viewCloudun.tag = 5;
        _viewCloudun.userInteractionEnabled = true;
        UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [_viewCloudun addGestureRecognizer:tapGestureRecognizer];
    }
    return  _viewCloudun;
}
- (UILabel *)labelCloudun
{
    if (_labelCloudun == nil) {
        _labelCloudun = [UILabel  new];
        [GlobalMethod setLabel:_labelCloudun widthLimit:0 isBold:true fontNum:14 textColor:COLOR_BLACK text:@"Cloudun Score"];
    }
    return  _labelCloudun;
}
- (UIButton *)btnCloudun
{
    if (_btnCloudun == nil) {
        _btnCloudun = [UIButton  new];
        _btnCloudun.widthHeight = XY(100, Ratio(30));
        _btnCloudun.backgroundColor = COLOR_PRIMARY;
        [_btnCloudun setTitle:@"Obtener" forState:UIControlStateNormal];
        _btnCloudun.titleLabel.fontNum = 14;
        [_btnCloudun setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [GlobalMethod setRoundView:_btnCloudun color:[UIColor clearColor] numRound:_btnCloudun.height/2 width:0];
        _btnCloudun.tag = 1;
        [_btnCloudun addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnCloudun;
}
- (UILabel *)labelCloudunScore
{
    if (_labelCloudunScore == nil) {
        _labelCloudunScore = [UILabel  new];
        [GlobalMethod setLabel:_labelCloudunScore widthLimit:0 isBold:true fontNum:20 textColor:COLOR_BLACK text:@"0>"];
    }
    return  _labelCloudunScore;
}
- (UIView *)viewCloudunTime
{
    if (_viewCloudunTime == nil) {
        _viewCloudunTime = [UIView  new];
        _viewCloudunTime.height = 18+6+6;
        _viewCloudunTime.backgroundColor = [UIColor clearColor];
        _viewCloudunTime.userInteractionEnabled = true;
        _viewCloudunTime.width = self.viewBack.width;
        [_viewCloudunTime addSubview:self.imageCloudunTime];
    }
    return  _viewCloudunTime;
}
- (UIImageView *)imageCloudunTime{
    if (!_imageCloudunTime) {
        _imageCloudunTime = [UIImageView new];
        _imageCloudunTime.image = [UIImage imageNamed:@"icon-mine-hint"];
        _imageCloudunTime.widthHeight = XY(10, 11);
        _imageCloudunTime.rightCenterY = XY(self.viewCloudunTime.width-LEFT_MARDIN, self.viewCloudunTime.height/2);
        _imageCloudunTime.hidden = true;
    }
    return _imageCloudunTime;
}
- (UILabel *)labelCloudunTime
{
    if (_labelCloudunTime == nil) {
        _labelCloudunTime = [UILabel  new];
        _labelCloudunTime.fontNum = 12;
        _labelCloudunTime.textColor = COLOR_LIGHT_GRAY;
    }
    return  _labelCloudunTime;
}
- (UIButton *)btnCloudunTime
{
    if (_btnCloudunTime == nil) {
        _btnCloudunTime = [UIButton  new];
        _btnCloudunTime.widthHeight = XY(90+LEFT_MARDIN, self.viewCloudunTime.height);
        _btnCloudunTime.tag = 3;
        [_btnCloudunTime addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnCloudunTime;
}
- (UIView *)viewFico
{
    if (_viewFico == nil) {
        _viewFico = [UIView  new];
        _viewFico.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
        [GlobalMethod setRoundView:_viewFico color:[UIColor clearColor] numRound:16 width:0];
        _viewFico.tag = 6;
        _viewFico.userInteractionEnabled = true;
        UITapGestureRecognizer * tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [_viewFico addGestureRecognizer:tapGestureRecognizer];
    }
    return  _viewFico;
}
- (UILabel *)labelFico
{
    if (_labelFico == nil) {
        _labelFico = [UILabel  new];
        [GlobalMethod setLabel:_labelFico widthLimit:0 isBold:true fontNum:14 textColor:COLOR_BLACK text:@"FICO Score"];
    }
    return  _labelFico;
}
- (UIButton *)btnFico
{
    if (_btnFico == nil) {
        _btnFico = [UIButton  new];
        _btnFico.widthHeight = XY(100, Ratio(30));
        _btnFico.backgroundColor = COLOR_PRIMARY;
        [_btnFico setTitle:@"Obtener" forState:UIControlStateNormal];
        [_btnFico setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        [GlobalMethod setRoundView:_btnFico color:[UIColor clearColor] numRound:_btnCloudun.height/2 width:0];
        _btnFico.titleLabel.fontNum = 14;
        _btnFico.tag = 2;
        [_btnFico addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnFico;
}
- (UIImageView *)imageFicoError{
    if (!_imageFicoError) {
        _imageFicoError = [UIImageView new];
        _imageFicoError.widthHeight = XY(16, 16);
        _imageFicoError.image = [UIImage imageNamed:@"icon-order-error"];
        _imageFicoError.hidden = true;
    }
    return _imageFicoError;
}
- (UILabel *)labelFicoScore
{
    if (_labelFicoScore == nil) {
        _labelFicoScore = [UILabel  new];
        [GlobalMethod setLabel:_labelFicoScore widthLimit:0 isBold:true fontNum:20 textColor:COLOR_BLACK text:@"666>"];
    }
    return  _labelFicoScore;
}
- (UIView *)viewFicoTime
{
    if (_viewFicoTime == nil) {
        _viewFicoTime = [UIView  new];
        _viewFicoTime.height = 18+6+6;
        _viewFicoTime.backgroundColor = [UIColor clearColor];
        _viewFicoTime.userInteractionEnabled = true;
        _viewFicoTime.width = self.viewBack.width;
        [_viewFicoTime addSubview:self.imageFicoTime];
    }
    return  _viewFicoTime;
}
- (UIImageView *)imageFicoTime{
    if (!_imageFicoTime) {
        _imageFicoTime = [UIImageView new];
        _imageFicoTime.image = [UIImage imageNamed:@"icon-mine-hint"];
        _imageFicoTime.widthHeight = XY(10, 11);
        _imageFicoTime.rightCenterY = XY(self.viewFicoTime.width-LEFT_MARDIN, self.viewFicoTime.height/2);
        _imageFicoTime.hidden = true;
    }
    return _imageFicoTime;
}
- (UILabel *)labelFicoTime
{
    if (_labelFicoTime == nil) {
        _labelFicoTime = [UILabel  new];
        _labelFicoTime.fontNum = 12;
        _labelFicoTime.textColor = COLOR_LIGHT_GRAY;
    }
    return  _labelFicoTime;
}
- (UIButton *)btnFicoTime
{
    if (_btnFicoTime == nil) {
        _btnFicoTime = [UIButton  new];
        _btnFicoTime.widthHeight = XY(90+LEFT_MARDIN, self.viewFicoTime.height);
        _btnFicoTime.tag = 4;
        [_btnFicoTime addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnFicoTime;
}
- (WXPReportFailurePopupWindowView *)viewPopup
{
    if (_viewPopup == nil) {
        _viewPopup = [WXPReportFailurePopupWindowView  new];
    }
    return  _viewPopup;
}
- (UILabel *)labelErrorInfo{
    if (!_labelErrorInfo) {
        _labelErrorInfo = [UILabel new];
        _labelErrorInfo.textColor = COLOR_RED;
        _labelErrorInfo.fontNum = 12;
        _labelErrorInfo.numberOfLines = 0;
        _labelErrorInfo.lineSpace = 3;
    }
    return _labelErrorInfo;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = SCREEN_WIDTH;
        [self addSubView];
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.lineTitle];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.lineView];
    [self.viewBack addSubview:self.viewCloudun];
    [self.viewCloudun addSubview:self.labelCloudun];
    [self.viewCloudun addSubview:self.btnCloudun];
    [self.viewCloudun addSubview:self.labelCloudunScore];
    [self.viewBack addSubview:self.viewCloudunTime];
    [self.viewCloudunTime addSubview:self.labelCloudunTime];
    [self.viewCloudunTime addSubview:self.btnCloudunTime];
    
    
    [self.viewBack addSubview:self.viewFico];
    [self.viewFico addSubview:self.labelFico];
    [self.viewFico addSubview:self.btnFico];
    [self.viewFico addSubview:self.imageFicoError];
    [self.viewFico addSubview:self.labelFicoScore];
    [self.viewBack addSubview:self.viewFicoTime];
    [self.viewFicoTime addSubview:self.labelFicoTime];
    [self.viewFicoTime addSubview:self.btnFicoTime];
    
    [self.viewBack addSubview:self.labelErrorInfo];
    
    //初始化页面
    [self resetViewWithModel:nil];
}

#pragma mark - 点击事件
- (void)tapClick:(UITapGestureRecognizer * )tap{
    if(tap.view.tag == 5){
        if (self.labelCloudunScore.hidden == false) {
            WXPCreditReportDetailVC * vc = [WXPCreditReportDetailVC new];
            vc.type = ENUM_REPORT_CLOUNDUN;
            [Global_Nav pushViewController:vc animated:true];
        }
    }
    if(tap.view.tag == 6){
        if (self.labelFicoScore.hidden == false) {
            WXPCreditReportDetailVC * vc = [WXPCreditReportDetailVC new];
            vc.type = ENUM_REPORT_FICO;
            [Global_Nav pushViewController:vc animated:true];
        }
    }
}
- (void)btnClick:(UIButton *)sender{
    switch (sender.tag) {
        case 1:
        {
            [NSObject clickFeedback];
            [sender addClickAnimate];
            WXPCreditReportDetailVC * vc = [WXPCreditReportDetailVC new];
            vc.type = ENUM_REPORT_CLOUNDUN;
            [Global_Nav pushViewController:vc animated:true];
        }
            break;
        case 2:
        {
            [NSObject clickFeedback];
            [sender addClickAnimate];
            WXPCreditReportDetailVC * vc = [WXPCreditReportDetailVC new];
            vc.type = ENUM_REPORT_FICO;
            [Global_Nav pushViewController:vc animated:true];
        }
            break;
        case 3:
        {
            if (self.imageCloudunTime.hidden == false) {
                [NSObject clickFeedback];
                [sender addClickAnimate];
                [self.viewPopup resetCloudunView];
                UIWindow * window = [UIApplication sharedApplication].windows.firstObject;
                [window endEditing:true];
                [window addSubview:self.viewPopup];
            }
        }
            break;
        case 4:
        {
            if (self.imageFicoTime.hidden == false) {
                [NSObject clickFeedback];
                [sender addClickAnimate];
                [self.viewPopup resetFicoView];
                UIWindow * window = [UIApplication sharedApplication].windows.firstObject;
                [window endEditing:true];
                [window addSubview:self.viewPopup];
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 刷新view
- (void)resetViewWithModel:(ModelGetCRSource *)model{
    self.labelTitle.leftTop = XY(LEFT_MARDIN, LEFT_MARDIN);
    self.lineTitle.leftBottom = XY(LEFT_MARDIN, self.labelTitle.bottom);
    self.lineView.widthHeight = XY(self.viewBack.width-self.labelTitle.left*2, 1);
    self.lineView.leftTop = XY(self.labelTitle.left, self.labelTitle.bottom+14);
    
    // Cloudun
    self.viewCloudun.widthHeight = XY(self.viewBack.width-self.labelTitle.left*2, Ratio(50));
    self.viewCloudun.centerXTop = XY(self.viewBack.width/2, self.lineView.bottom+19);
    self.labelCloudun.leftCenterY = XY(LEFT_MARDIN, self.viewCloudun.height/2);
    self.btnCloudun.rightCenterY = XY(self.viewCloudun.width-LEFT_MARDIN, self.viewCloudun.height/2);
    self.btnCloudun.hidden = true;
    if(model.isHaveCloudunScore==false){
        self.btnCloudun.hidden = false;
    }
    self.labelCloudunScore.hidden = !self.btnCloudun.hidden;
    if(self.labelCloudunScore.hidden == false){
        [self.labelCloudunScore fitTitle:[NSString stringWithFormat:@"%@>",[GlobalMethod showDoubleToStr:model.cloudunScore]] variable:0];
        self.labelCloudunScore.rightCenterY = XY(self.viewCloudun.width-LEFT_MARDIN, self.viewCloudun.height/2);
    }
    CGFloat markTop = self.viewCloudun.bottom;
    
    //Cloudun - Time
    self.viewCloudunTime.hidden = true;
    // 返回时间就显示
    self.viewCloudunTime.hidden = model.cloudunExpireTime <= 0;
    if(self.viewCloudunTime.hidden == false){
        self.imageCloudunTime.hidden = ![self checkExpireTime:model.cloudunExpireTime];
        self.viewCloudunTime.leftTop = XY(0, markTop);
        NSString * cloudunTime = [GlobalMethod exchangeTimestamp:strDotF(model.cloudunExpireTime) formate:TIME_DAY_MONTH_YEAR];
        NSString * strTime = [NSString stringWithFormat:@"Fecha de emisión del informe: %@",cloudunTime];
        [self.labelCloudunTime setAttributedText:[strTime setDifferentColorWithMarkStr:cloudunTime lineSpacing:0 alignment:NSTextAlignmentRight color:COLOR_RED]];
        [self.labelCloudunTime sizeToFit];
        self.labelCloudunTime.rightCenterY = XY(self.imageCloudunTime.hidden == true ? self.imageCloudunTime.right: self.imageCloudunTime.left-4, self.viewCloudunTime.height/2);
        self.btnCloudunTime.rightTop = XY(self.viewCloudunTime.width, 0);
    }
    markTop = self.viewCloudunTime.hidden ? markTop : self.viewCloudunTime.bottom;
    
    // FICO
    self.viewFico.hidden = !model.ficoshow;
    self.viewFico.widthHeight = self.viewCloudun.widthHeight;
    self.viewFico.centerXTop = XY(self.viewBack.width/2, self.viewCloudunTime.hidden ? markTop+LEFT_MARDIN : markTop);
    self.labelFico.leftCenterY = XY(LEFT_MARDIN, self.viewFico.height/2);
    self.btnFico.rightCenterY = XY(self.viewFico.width-LEFT_MARDIN, self.viewFico.height/2);
    self.btnFico.hidden = true;
    if((model.ficoInfoError==false && model.isHaveFicoScore == false)){
        // 显示Obtener按钮
        self.btnFico.hidden = false;
    }
    self.labelFicoScore.hidden = !self.btnFico.hidden;
    self.imageFicoError.hidden = true;
    if(model.ficoInfoError){
        self.labelFicoScore.textColor = COLOR_RED;
        [self.labelFicoScore fitTitle:@"???>" variable:0];
        self.imageFicoError.hidden = false;
        self.imageFicoError.leftCenterY = XY(self.labelFico.left, self.viewFico.height/2);
        self.labelFico.leftCenterY = XY(self.imageFicoError.right+8, self.viewFico.height/2);
    }else{
        self.labelFicoScore.textColor = COLOR_BLACK;
        [self.labelFicoScore fitTitle:[NSString stringWithFormat:@"%@>",[GlobalMethod showDoubleToStr:model.ficoScore]] variable:0];
    }
    self.labelFicoScore.rightCenterY = XY(self.viewFico.width-LEFT_MARDIN, self.viewFico.height/2);
    
    markTop = self.viewFico.hidden ? markTop : self.viewFico.bottom;
    
    // FICO - Time
    self.viewFicoTime.hidden = true;
//    self.viewFicoTime.hidden = !self.btnFico.hidden;
    self.viewFicoTime.hidden = model.ficoExpireTime <= 0;
//    if(self.viewFicoTime.hidden == false){
//        if(model.ficoInfoError == true){
//            // 此处，时间和下面的错误信息不能同时显示
//            self.viewFicoTime.hidden = true;
//        }
//    }
//    if(self.viewFico.hidden == true){
//        self.viewFicoTime.hidden = true;
//    }
    
    if(self.viewFicoTime.hidden == false){
        self.imageFicoTime.hidden = ![self checkExpireTime:model.ficoExpireTime];
        self.viewFicoTime.leftTop = XY(0, markTop);
        NSString * ficoTime = [GlobalMethod exchangeTimestamp:strDotF(model.ficoExpireTime) formate:TIME_DAY_MONTH_YEAR];
        NSString * strTime = [NSString stringWithFormat:@"Fecha de emisión del informe: %@",ficoTime];
        [self.labelFicoTime setAttributedText:[strTime setDifferentColorWithMarkStr:ficoTime lineSpacing:0 alignment:NSTextAlignmentRight color:COLOR_RED]];
        [self.labelFicoTime sizeToFit];
        self.labelFicoTime.rightCenterY = XY(self.imageFicoTime.hidden == true ? self.imageFicoTime.right: self.imageFicoTime.left-4, self.viewFicoTime.height/2);
        self.btnFicoTime.rightTop = XY(self.viewFicoTime.width, 0);
    }
    markTop = self.viewFicoTime.hidden ? markTop : self.viewFicoTime.bottom;
    
    // 错误信息
    self.labelErrorInfo.hidden = !model.ficoInfoError;
    [self.labelErrorInfo fitTitle:@"El puntaje FICO ha sido pagado, pero no se ha generado ningún informe. Por favor, actualice su información personal para aprobarlo de nuevo" variable:self.viewFico.width];
    self.labelErrorInfo.leftTop = XY(self.viewFico.left, markTop+10);
    markTop = self.labelErrorInfo.hidden ? markTop : self.labelErrorInfo.bottom;
    
    self.viewBack.height = markTop+LEFT_MARDIN;
    self.viewBack.centerXTop = XY(self.width/2, 0);
    self.height = self.viewBack.bottom+LEFT_MARDIN;
}

// 判断时间是否过期或即将过期
- (BOOL)checkExpireTime:(int64_t)timestamp {
    int64_t currentTime = (int64_t)([[NSDate date] timeIntervalSince1970] * 1000);
    int64_t oneWeekInMilliseconds = 7 * 24 * 60 * 60 * 1000;
    return currentTime >= timestamp || (timestamp - currentTime) <= oneWeekInMilliseconds;
}


@end
